package net.java.dev.netbeansspot;

import com.sun.spot.peripheral.radio.RadioFactory;
import com.sun.spot.resources.Resources;
import com.sun.spot.resources.transducers.IIOPin;
import com.sun.spot.resources.transducers.IInputPin;
import com.sun.spot.resources.transducers.IToneGenerator;
import com.sun.spot.resources.transducers.ITriColorLED;
import com.sun.spot.sensorboard.EDemoBoard;
import com.sun.spot.sensorboard.io.IScalarInput;
import com.sun.spot.sensorboard.peripheral.ToneGenerator;
import com.sun.spot.util.*;
import java.io.IOException;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class Application extends MIDlet {

    private ITriColorLED[] leds = EDemoBoard.getInstance().getLEDs();
    int range;
    
    private boolean flag;
    private IToneGenerator speaker = (IToneGenerator) Resources.lookup(IToneGenerator.class, "speaker");
    private IIOPin pinDN = (IIOPin) Resources.lookup(IIOPin.class, "D1");

    //private IIOPin pinDN = (IIOPin) Resources.lookup(IIOPin.class, "D0");
    
    void emitirSenalSonar() {
        
        /**
         *  Distancias:
         *  0,5m -> 74
         *  1,0m -> 114
         *  2,0m -> 152
         **/
        
        IScalarInput rawAnalog = EDemoBoard.getInstance().getScalarInputs()[EDemoBoard.A0];
        //int pulso = EDemoBoard.getInstance().getPulse(pinDN, true, 0);

        while (true) {
            Utils.sleep(300);
            try {
                range = rawAnalog.getValue();
                //System.out.println("Rango: "+range+" - Pulso: "+pulso);
                System.out.println("rango: " + range);
                //if (range >=74 && range <=114){
                if (range>= 114 && range <=152){
                    activarAlarmaProximidad();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            showGauge(range);
        }
    }

    void activarAlarmaProximidad() {
            System.out.println("prueba ALARAMA de sonar");
            //tono en (frecuencia[Hz],duracion[ms])
            speaker.startTone(2500, 300);
            Utils.sleep(600);
            speaker.startTone(2500, 300);
            Utils.sleep(600);
            speaker.startTone(2500, 300);
            Utils.sleep(200);
    }

    void valoresIniciales(){
        speaker = new ToneGenerator(pinDN);
    }
    
    protected void startApp() throws MIDletStateChangeException {
        //System.out.println("Hello, world");
        //new BootloaderListener().start();   // monitor the USB (if connected) and recognize commands from host

        //long ourAddr = RadioFactory.getRadioPolicyManager().getIEEEAddress();
        //System.out.println("Our radio address = " + IEEEAddress.toDottedHex(ourAddr));
        /*speaker = new ToneGenerator(pinDN);
        emitirSenalSonar();*/
        valoresIniciales();
        //emitirSenalSonar();
        
        new Thread(){
            public void run(){
                emitirSenalSonar();
            }
        }.start();
        /*
        IScalarInput rawAnalog = EDemoBoard.getInstance().getScalarInputs()[EDemoBoard.A0];
        //int pulso = EDemoBoard.getInstance().getPulse(pinDN, true, 0);

        while (true) {
            Utils.sleep(300);
            try {
                range = rawAnalog.getValue();
                //System.out.println("Rango: "+range+" - Pulso: "+pulso);
                System.out.println("rango: " + range);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            showGauge(range);
        }*/

    }

    protected void pauseApp() {
        // This is not currently called by the Squawk VM
    }

    private void showGauge(int range) {
        if (range < 20) {
            for (int i = 0; i < 8; i++) {
                leds[i].setOff();
            }
        } else if ((range >= 20) && (range < 80)) {
            for (int i = 1; i < 8; i++) {
                leds[i].setOff();
            }
            leds[0].setRGB(100, 0, 0);
            leds[0].setOn();
        } else if ((range >= 80) && (range < 140)) {
            for (int i = 2; i < 8; i++) {
                leds[i].setOff();
            }
            leds[0].setRGB(100, 0, 0);
            leds[0].setOn();
            leds[1].setRGB(88, 13, 0);
            leds[1].setOn();
        } else if ((range >= 140) && (range < 200)) {
            leds[0].setRGB(100, 0, 0);
            leds[0].setOn();
            leds[1].setRGB(88, 13, 0);
            leds[1].setOn();
            leds[2].setRGB(75, 25, 0);
            leds[2].setOn();
            for (int i = 3; i < 8; i++) {
                leds[i].setOff();
            }
        } else if ((range >= 200) && (range < 260)) {
            leds[0].setRGB(100, 0, 0);
            leds[0].setOn();
            leds[1].setRGB(88, 13, 0);
            leds[1].setOn();
            leds[2].setRGB(75, 25, 0);
            leds[2].setOn();
            leds[3].setRGB(63, 38, 0);
            leds[3].setOn();
            for (int i = 4; i < 8; i++) {
                leds[i].setOff();
            }
        } else if ((range >= 320) && (range < 380)) {
            leds[0].setRGB(100, 0, 0);
            leds[0].setOn();
            leds[1].setRGB(88, 13, 0);
            leds[1].setOn();
            leds[2].setRGB(75, 25, 0);
            leds[2].setOn();
            leds[3].setRGB(63, 38, 0);
            leds[3].setOn();
            leds[4].setRGB(50, 50, 0);
            leds[4].setOn();
            for (int i = 5; i < 8; i++) {
                leds[i].setOff();
            }
        } else if ((range >= 380) && (range < 440)) {
            leds[0].setRGB(100, 0, 0);
            leds[0].setOn();
            leds[1].setRGB(88, 13, 0);
            leds[1].setOn();
            leds[2].setRGB(75, 25, 0);
            leds[2].setOn();
            leds[3].setRGB(63, 38, 0);
            leds[3].setOn();
            leds[4].setRGB(50, 50, 0);
            leds[4].setOn();
            leds[5].setRGB(38, 63, 0);
            leds[5].setOn();
            for (int i = 6; i < 8; i++) {
                leds[i].setOff();
            }
        } else if ((range >= 440) && (range < 480)) {
            leds[0].setRGB(100, 0, 0);
            leds[0].setOn();
            leds[1].setRGB(88, 13, 0);
            leds[1].setOn();
            leds[2].setRGB(75, 25, 0);
            leds[2].setOn();
            leds[3].setRGB(63, 38, 0);
            leds[3].setOn();
            leds[4].setRGB(50, 50, 0);
            leds[4].setOn();
            leds[5].setRGB(38, 63, 0);
            leds[5].setOn();
            leds[6].setRGB(25, 75, 0);
            leds[6].setOn();

            leds[7].setOff();
        } else if (range >= 480) {
            leds[0].setRGB(100, 0, 0);
            leds[0].setOn();
            leds[1].setRGB(88, 13, 0);
            leds[1].setOn();
            leds[2].setRGB(75, 25, 0);
            leds[2].setOn();
            leds[3].setRGB(63, 38, 0);
            leds[3].setOn();
            leds[4].setRGB(50, 50, 0);
            leds[4].setOn();
            leds[5].setRGB(38, 63, 0);
            leds[5].setOn();
            leds[6].setRGB(25, 75, 0);
            leds[6].setOn();
            leds[7].setRGB(0, 100, 0);
            leds[7].setOn();
        }
    }

    protected void destroyApp(boolean bln) throws MIDletStateChangeException {
        for (int i = 0; i < 8; i++) {
            leds[i].setOff();
        }
    }
}
/**
 * Called if the MIDlet is terminated by the system. I.e. if startApp throws any
 * exception other than MIDletStateChangeException, if the isolate running the
 * MIDlet is killed with Isolate.exit(), or if VM.stopVM() is called.
 *
 * It is not called if MIDlet.notifyDestroyed() was called.
 *
 * @param unconditional If true when this method is called, the MIDlet must
 * cleanup and release all resources. If false the MIDlet may throw
 * MIDletStateChangeException to indicate it does not want to be destroyed at
 * this time.
 */
